package com.yuji.aacar.net.convertfactory;

import android.util.Base64;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.yuji.aacar.net.config.BlueLog;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Converter;

/**
 * <li>Package:consumer.ttpc.com.httpmodule.converterfactory
 * <li>Author: yehaijian
 * <li>Date: 2017/6/7
 * <li>Description:
 */
public class JsonBase64ResponseConverter<T>  implements Converter<ResponseBody, T> {
    private final Gson mGson;//gson对象
    private final TypeAdapter<T> adapter;

    /**
     * 构造器
     */
    public JsonBase64ResponseConverter(Gson gson, TypeAdapter<T> adapter) {
        this.mGson = gson;
        this.adapter = adapter;
    }

    /**
     * 转换
     *
     * @param responseBody 业务对象
     * @return
     * @throws IOException
     */
    @Override
    public T convert(ResponseBody responseBody) throws IOException {

        String response = responseBody.string();
        String result=new String(Base64.decode(response, Base64.NO_WRAP));

        BlueLog.e("result", "result ##：" + result);
        return adapter.fromJson(result);
    }
}
